﻿using log4net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CAD
{
    public class DAOCliente : DAO
    {
        ILog logger = LogManager.GetLogger(typeof(DAOCliente));     


       #region Attributes
       private static DAOCliente singleton;
       #endregion 

       #region Singleton
        public static DAOCliente Instance()
        {
            if (singleton == null)
            {
                singleton = new DAOCliente();
            }
            return singleton;
        }
        protected DAOCliente()
        {
        }
        #endregion 

        #region Querys
        public List<DTOCliente> VerificarUsuario(string ususario, string contrasena)
        {
            using (DbCommand cmd = defaultDB.GetStoredProcCommand("PAR.cliente_VerificarUsuario"))
            {
                defaultDB.AddInParameter(cmd, "@ususario", DbType.String, ususario);
                defaultDB.AddInParameter(cmd, "@contrasena", DbType.String, contrasena);
                using (IDataReader reader = defaultDB.ExecuteReader(cmd))
                {
                    logger.Info("Se verifico los usuarios");
                    return CargarLista(reader);
                }
            }
        }
        public List<DTOCliente> SelectAll()
        {
            using (DbCommand cmd = defaultDB.GetStoredProcCommand("Cliente_SelectAll"))
            {
                using (IDataReader reader = defaultDB.ExecuteReader(cmd))
                {
                    logger.Info("seleccion de clientes");
                    return CargarLista(reader);
                }
            }
        }
        #endregion

        #region Helpers
        private List<DTOCliente> CargarLista(IDataReader reader)
        {
            List<DTOCliente> list = new List<DTOCliente>();
            while (reader.Read())
            {
                list.Add(CargarItem(reader));
            }
            return list;
        }
        private DTOCliente CargarItem(IDataReader reader)
        {
            DTOCliente item = new DTOCliente();
            item.IDCliente = reader.GetInt32(0);
            item.Usuario = reader.GetString(1);
            item.Password = reader.GetString(2);
            logger.Info("Se cargaron los parametros de usuario");
            return item;
        }
        #endregion

    }
}
